package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("checkgroup")
public class CheckGroupController {

    @DubboReference
    CheckGroupService checkGroupService;

    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean){
        /*
        * @Date: 4:23 下午 2021/1/4
        * @param: [queryPageBean]
        * @return: com.itheima.health.entity.Result
        * @Description: 检查组findPage
        */
        log.info("[预约-检查组-findPage-前端传入数据] - queryPageBean - {}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        log.info("[预约-检查组-findPage-后端返回数据] - pageResult - {}",pageResult);

        if (pageResult==null){
            return new Result(false, MessageConst.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }


    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        /*
        * @Date: 1:42 下午 2021/1/5
        * @param: [checkGroup, checkitemIds]
        * @return: com.itheima.health.entity.Result
        * @Description: 添加检查组
        */
        log.info("[预约-检查组-add-前端传入数据] - checkGroup - {}",checkGroup);
        log.info("[预约-检查组-add-前端传入数据] - checkitemIds - {}", Arrays.toString(checkitemIds));

        boolean flag = checkGroupService.add(checkGroup,checkitemIds);
        if (!flag){
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL);
        }

        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);

    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @GetMapping("delete")
    public Result delete(Integer id){
        /*
        * @Date: 1:42 下午 2021/1/5
        * @param: [id]
        * @return: com.itheima.health.entity.Result
        * @Description: 删除检查组
        */
        log.info("[预约-检查组-delete-前端传入数据] - id - {}",id);
        boolean flag = checkGroupService.delete(id);
        if (!flag){
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }


    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id){
        /*
        * @Date: 1:42 下午 2021/1/5
        * @param: [id]
        * @return: com.itheima.health.entity.Result
        * @Description: 根据id查询检查组
        */
        log.info("[预约-检查组-findById-前端传入数据] - id - {}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        log.info("[预约-检查组-findById-后端返回数据] - checkGroup - {}",checkGroup);

        if (checkGroup==null){
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }


    @GetMapping("findByCheckGroupIdAndCheckItemId")
    public Result findByCheckGroupIdAndCheckItemId(Integer id){
        /*
        * @Date: 2:47 下午 2021/1/5
        * @param: [id]
        * @return: com.itheima.health.entity.Result
        * @Description: 根据CheckGroupId查询CheckItemId
        */
        log.info("[预约-检查组-findByCheckGroupIdAndCheckItemId-前端传入数据] - id - {}",id);
        List<Integer> checkItemIds = checkGroupService.findByCheckGroupIdAndCheckItemId(id);
        log.info("[预约-检查组-findByCheckGroupIdAndCheckItemId-后端返回数据] - checkItemIds - {}",checkItemIds);

        if (checkItemIds==null){
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PostMapping("update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[预约-检查组-update-前端传入数据] - checkGroup - {}",checkGroup);
        log.info("[预约-检查组-update-前端传入数据] - checkitemIds - {}", Arrays.toString(checkitemIds));

        boolean flag = checkGroupService.update(checkGroup,checkitemIds);
        if (!flag){
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }


    @GetMapping("findAll")
    public Result findAll(){
        /*
        * @Date: 11:06 上午 2021/1/6
        * @param: []
        * @return: com.itheima.health.entity.Result
        * @Description:查询所有的CheckGroup
        */
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if (checkGroupList==null){
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }

}
